package com.example.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.example.domain.NianXiuJiaEntity;
import com.example.mapper.NianXiuJiaMapper;
import com.example.service.NianXiuJiaService;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class NianXiuJiaServiceImp implements NianXiuJiaService {

	@Autowired
	NianXiuJiaMapper nianXiuJiaMapper;

	@Override
	public String queryQingjiaPerson(String year, String person, String deptId, String date) {
		List<NianXiuJiaEntity> personList = new ArrayList<NianXiuJiaEntity>();
		personList = nianXiuJiaMapper.queryQingjiaPerson(year,person,deptId);
		Map<String, Object> finalMap = changeData2MapStr(personList);
		if(date !=null && !"".equals(date)) {
			Map<String,List<NianXiuJiaEntity>> map = (Map<String, List<NianXiuJiaEntity>>) finalMap.get("date");
			if(map.get(date)==null) {
				return JSONObject.toJSONString("[]");
			}else {
				return JSONObject.toJSONString(map.get(date));
			}
		}else {
			return JSONObject.toJSONString(finalMap.get("list"));
		}
		
	}
	
	/**
	 * 将数据库查询到的数据转换成前端需要的数据
	 * @param personList
	 * @return
	 */
	private Map<String, Object> changeData2MapStr(List<NianXiuJiaEntity> personList) {
		
		Map<String, Object> finalMap = new HashMap<String, Object>();
				
		List<NianXiuJiaEntity> personShowList = new ArrayList<NianXiuJiaEntity>();// 用于表格展示的list
		Map<String,List<NianXiuJiaEntity>> personMap = new HashMap<String, List<NianXiuJiaEntity>>(); // map格式,key:人员信息
		Map<String,List<NianXiuJiaEntity>> dateMap = new HashMap<String, List<NianXiuJiaEntity>>(); // map格式,key:日期
		Map<String,Map<String,List<NianXiuJiaEntity>>> deptMap = new HashMap<String, Map<String,List<NianXiuJiaEntity>>>();// map格式,key:部门，value:{月:人}
		for (NianXiuJiaEntity s : personList) {
			String deptId = s.getDeptid();
			String deptName = s.getDeptname();
			Map<String,List<NianXiuJiaEntity>> monthMap = getDeptMapValue(deptMap, deptId+"_"+deptName); //获取该部门 对应 月份的 年休假名单
			
			List<NianXiuJiaEntity> onePersonMapDateList = personMap.get(s.getPersonid()+"_"+s.getPersonname());
			if(onePersonMapDateList == null) {
				onePersonMapDateList = new ArrayList<NianXiuJiaEntity>();
			}
			if(s.getQingjiaStartdate() != null && !"".equals(s.getQingjiaStartdate()) && 
					s.getQingjiaEnddate() != null && !"".equals(s.getQingjiaEnddate())) {
				int count = dateMinus(s.getQingjiaStartdate(), s.getQingjiaEnddate());
				if (count == 0) {
					s.setTongjiDate(s.getQingjiaStartdate());
					onePersonMapDateList.add(s);
					personShowList.add(s);
					// 日期格式的
					List<NianXiuJiaEntity> oneDateMapDateList = getDateMapValue(dateMap,s.getQingjiaStartdate());
					oneDateMapDateList.add(s);
					dateMap.put(s.getQingjiaStartdate(), oneDateMapDateList);
					// 部门格式的
					int month = Integer.parseInt(s.getQingjiaStartdate().split("-")[1]);
					List<NianXiuJiaEntity> oneDeptMapDateList = monthMap.get(month+"");
					oneDeptMapDateList.add(s);
					monthMap.put(month+"", oneDeptMapDateList);
				} else {
					s.setTongjiDate(s.getQingjiaStartdate());
					onePersonMapDateList.add(s);
					personShowList.add(s);
					// 日期格式的
					List<NianXiuJiaEntity> oneDateMapDateBeginList = getDateMapValue(dateMap,s.getQingjiaStartdate());
					oneDateMapDateBeginList.add(s);
					dateMap.put(s.getQingjiaStartdate(), oneDateMapDateBeginList);
					// 部门格式的
					int month = Integer.parseInt(s.getQingjiaStartdate().split("-")[1]);
					List<NianXiuJiaEntity> oneDeptMapDateBeginList = monthMap.get(month+"");
					oneDeptMapDateBeginList.add(s);
					monthMap.put(month+"", oneDeptMapDateBeginList);
					
					Date ksDate = formatStr2Date(s.getQingjiaStartdate());
					for (int k = 1; k < count; k++) {
						ksDate = getNextDay(ksDate, 1);
						String d = formatDate(ksDate);
						NianXiuJiaEntity newEntity = (NianXiuJiaEntity) s.clone();
						newEntity.setTongjiDate(d);
						onePersonMapDateList.add(newEntity);
						personShowList.add(newEntity);
						// 日期格式的
						List<NianXiuJiaEntity> oneDateMapDateMiddleList = getDateMapValue(dateMap, d);
						oneDateMapDateMiddleList.add(newEntity);
						dateMap.put(d, oneDateMapDateMiddleList);
						// 部门格式的
						month = Integer.parseInt(d.split("-")[1]);
						List<NianXiuJiaEntity> oneDeptMapDateMiddleList = monthMap.get(month+"");
						oneDeptMapDateMiddleList.add(newEntity);
						monthMap.put(month+"", oneDeptMapDateMiddleList);
					}
					NianXiuJiaEntity endEntity = (NianXiuJiaEntity) s.clone();
					endEntity.setTongjiDate(s.getQingjiaEnddate());
					onePersonMapDateList.add(endEntity);
					personShowList.add(endEntity);
					// 日期格式的
					List<NianXiuJiaEntity> oneDateMapDateEndList = getDateMapValue(dateMap,s.getQingjiaEnddate());
					oneDateMapDateEndList.add(endEntity);
					dateMap.put(s.getQingjiaEnddate(), oneDateMapDateEndList);
					// 部门格式的
					month = Integer.parseInt(s.getQingjiaEnddate().split("-")[1]);
					List<NianXiuJiaEntity> oneDeptMapDateEndList = monthMap.get(month+"");
					oneDeptMapDateEndList.add(endEntity);
					monthMap.put(month+"", oneDeptMapDateEndList);
				}
			}
			
			deptMap.put(deptId+"_"+deptName, monthMap);
			personMap.put(s.getPersonid()+"_"+s.getPersonname(), onePersonMapDateList);
		}
		log.info("用于表格格式展示:{}", JSONObject.toJSONString(personShowList));
		log.info("根据人员的筛选:{}", JSONObject.toJSONString(personMap));
		log.info("根据日期的筛选:{}", JSONObject.toJSONString(dateMap));
		log.info("根据部门的筛选:{}", JSONObject.toJSONString(deptMap));
		finalMap.put("list", personShowList);
		finalMap.put("person", personMap);
		finalMap.put("date", dateMap);
		finalMap.put("dept", deptMap);
		return finalMap;
	}

	private List<NianXiuJiaEntity> getDateMapValue(Map<String,List<NianXiuJiaEntity>> map,String key){
		List<NianXiuJiaEntity> mapList = map.get(key);
		if(mapList == null) {
			mapList = new ArrayList<NianXiuJiaEntity>();
		}
		return mapList;
	}
	
	private Map<String,List<NianXiuJiaEntity>> getDeptMapValue(Map<String,Map<String,List<NianXiuJiaEntity>>> map,String key){
		Map<String,List<NianXiuJiaEntity>> mapList = map.get(key);
		if(mapList == null) {
			mapList = initMonthMap();
		}
		return mapList;
	}
	/**
	 * 初始化月份的map
	 * @return
	 */
	private Map<String,List<NianXiuJiaEntity>> initMonthMap() {
		Map<String,List<NianXiuJiaEntity>> monthMap = new HashMap<String,List<NianXiuJiaEntity>>(); 
		for(int i = 1 ; i <= 12 ; i++) {
			monthMap.put(i+"", new ArrayList<NianXiuJiaEntity>());
		}
		return monthMap;
	}
	/**
	 * 判断俩日期相差天数
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	private static int dateMinus(String dateStart, String dateEnd) {
		Timestamp t1 = Timestamp.valueOf(dateStart + " 00:00:00");
		Timestamp t2 = Timestamp.valueOf(dateEnd + " 00:00:00");
		Long days = t2.getTime() - t1.getTime();
		double day = Math.floor(days / (1000 * 60 * 60 * 24));
		return (int) day;
	}

	/**
	 * 获取当前日期的t天后
	 * 
	 * @param ksDate
	 * @param t
	 * @return
	 */
	private static Date getNextDay(Date ksDate, int t) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(ksDate);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime(); // 格式为"2019-02-16 00:00:00"
	}

	/**
	 * 将时间转换成字符串
	 * 
	 * @param date
	 * @return
	 */
	private static String formatDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
		return sdf.format(date);
	}

	/**
	 * 将日期字符串转换成实际
	 * 
	 * @param dateStr
	 * @return
	 */
	private static Date formatStr2Date(String dateStr) {
		Timestamp timestamp = Timestamp.valueOf(dateStr + " 00:00:00");
		return new Date(timestamp.getTime());
	}

	public static void main(String[] args) {

	}
}
